/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jdRegistrarRoles.java
 *
 * Created on 13-ago-2010, 12:08:14
 */

package GestionRoles;

import administracion.BeanBinding.TblBeanBindingFunciones;
import administracion.delegate.GestionSeguridadDelegate;
import administracion.helper.MiTablePrint;
import administracion.helper.Utiles_MensajesDialogo;
import administracion.helper.Utiles_Tabla;
import java.awt.Frame;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.BindingGroup;
import org.jdesktop.beansbinding.ELProperty;
import org.jdesktop.swingbinding.JTableBinding;
import org.jdesktop.swingbinding.SwingBindings;
import seguridad.administracion.ejb30.TO.DetalleFuncionRolTO;
import seguridad.administracion.ejb30.TO.DetalleUsuarioRolTO;
import seguridad.administracion.ejb30.TO.FuncionRolTO;
import seguridad.administracion.ejb30.TO.RolTO;
import seguridad.administracion.ejb30.TO.SistemaTO;
import seguridad.administracion.ejb30.entity.DetalleRolFuncion;
import seguridad.administracion.ejb30.entity.DetalleRolFuncionPK;
import seguridad.administracion.ejb30.entity.FuncionesRol;
import seguridad.administracion.ejb30.entity.FuncionesRolPK;
import seguridad.administracion.ejb30.entity.Roles;

/**
 *
 * @author Diana
 */
public class jdAsignarRoles extends javax.swing.JDialog {
    List<TblBeanBindingFunciones> funciones;
    List<TblBeanBindingFunciones> funcioneseliminar=new ArrayList<TblBeanBindingFunciones>();
    List<TblBeanBindingFunciones> funcionesagregar=new ArrayList<TblBeanBindingFunciones>();
    private JTableBinding jTableBinding;
    List<DetalleFuncionRolTO> detallesfunciones;
    List listaroles = null;
    jdAsignarFuncionUsuario padreasignar;
    /** Creates new form jdRegistrarRoles */
    public jdAsignarRoles(java.awt.Frame parent, boolean modal,jdAsignarFuncionUsuario padreasignar) {
        super(parent, modal);
        initComponents();
        this.llenarComboRoles();
        funciones= new ArrayList<TblBeanBindingFunciones>();
        this.padreasignar=padreasignar;
    }
     private void llenarComboRoles() {

        List listaTipos_cbo = this.cboBeanBindingRoles1.getLista();
        listaTipos_cbo.clear();
        try
        {
         listaroles = GestionSeguridadDelegate.getInstance().getListaRoles();

          if(!listaroles.isEmpty()){
              this.cboBeanBindingRoles1.setLista(listaroles);
              this.bindingGroup.bind();
              this.jcbRoles.updateUI();
              this.jcbRoles.setSelectedIndex(-1);
          }
          else{
              this.jcbRoles.removeAllItems();
          }
        } catch (Exception ex) {
          Utiles_MensajesDialogo.mensajeAlerta("Error al obtener roles\n" + ex.getMessage());
          ex.printStackTrace();
          Logger.getLogger(jdRegistrarFuncion.class.getName()).log(Level.SEVERE, null, ex);
        }
      }
    
    public void actualizaTablaDetalle() {
        this.bindingGroup = new BindingGroup();
        this.jTableBinding = SwingBindings.createJTableBinding(AutoBinding.UpdateStrategy.READ, this.funciones, this.jtFunciones);

        JTableBinding.ColumnBinding columnBinding0 = this.jTableBinding.addColumnBinding(ELProperty.create("${sistema}"));

        columnBinding0.setColumnName("SISTEMA");
        columnBinding0.setColumnClass(String.class);
        columnBinding0.setEditable(false);

        JTableBinding.ColumnBinding columnBinding1 = this.jTableBinding.addColumnBinding(ELProperty.create("${descripcion}"));

        columnBinding1.setColumnName("DESCRIPCION");
        columnBinding1.setColumnClass(String.class);
        columnBinding1.setEditable(false);

    

        this.bindingGroup.addBinding(this.jTableBinding);
        this.bindingGroup.bind();

        int[] porcentajes= {30,70};
        try {
            Utiles_Tabla.definirAnchoColumnasJTable(this.jtFunciones, porcentajes);
        } catch (Exception ex) {
            Logger.getLogger(jdAdministrarFuncion.class.getName()).log(Level.SEVERE, null, ex);
        }
  }
  
    public void cargarDetalles(){
        try {
            detallesfunciones = GestionSeguridadDelegate.getInstance().obtenerDetalleFuncionRol(this.obtenerRol(this.jcbRoles.getSelectedItem().toString()).getCodigo());
            funciones= this.convertirDetalles_Funciones(detallesfunciones);
        } catch (Exception ex) {
            Logger.getLogger(jdAsignarRoles.class.getName()).log(Level.SEVERE, null, ex);
        }
        actualizaTablaDetalle();

      }
    public List<TblBeanBindingFunciones> convertirDetalles_Funciones( List<DetalleFuncionRolTO> listadetalles){
           List<TblBeanBindingFunciones> deIngreso= new ArrayList<TblBeanBindingFunciones>();
          for(DetalleFuncionRolTO deta: listadetalles){
                 TblBeanBindingFunciones ing= new TblBeanBindingFunciones();
                 ing.setCodFuncion(deta.getCodfuncion());
                 ing.setCodsistema(deta.getCodsistema());
                 ing.setDescripcion(deta.getFuncion());
                 ing.setSistema(deta.getSistema());
                 deIngreso.add(ing);
             }
          return deIngreso;
      }
     public RolTO obtenerRol(String nombre){
        RolTO sis= new RolTO();
        for(int i=0 ; i<this.listaroles.size(); i++){
            if((((RolTO)listaroles.get(i)).getDescripcion().trim()).compareTo(nombre)==0){
                sis= (RolTO)listaroles.get(i);
            }
        }
        return sis;
    }
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        cboBeanBindingRoles1 = new administracion.BeanBinding.cboBeanBindingRoles();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtFunciones = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jcbRoles = new javax.swing.JComboBox();
        jbAceptar = new javax.swing.JButton();
        jbCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(gestionseguridadadministrativacliente.GestionSeguridadAdministrativaclienteApp.class).getContext().getResourceMap(jdAsignarRoles.class);
        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jtFunciones.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "DESCRIPCION DE FUNCIONES ASIGNADAS"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jtFunciones.setEnabled(false);
        jtFunciones.setName("jtFunciones"); // NOI18N
        jScrollPane2.setViewportView(jtFunciones);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 449, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 312, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jLabel2.setName("jLabel2"); // NOI18N

        jcbRoles.setName("jcbRoles"); // NOI18N

        org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${lista}");
        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, cboBeanBindingRoles1, eLProperty, jcbRoles);
        bindingGroup.addBinding(jComboBoxBinding);

        jcbRoles.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbRolesActionPerformed(evt);
            }
        });
        jcbRoles.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jcbRolesKeyPressed(evt);
            }
        });

        jbAceptar.setFont(resourceMap.getFont("jbAceptar.font")); // NOI18N
        jbAceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/button_ok.png"))); // NOI18N
        jbAceptar.setMnemonic('A');
        jbAceptar.setText(resourceMap.getString("jbAceptar.text")); // NOI18N
        jbAceptar.setName("jbAceptar"); // NOI18N
        jbAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAceptarActionPerformed(evt);
            }
        });
        jbAceptar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jbAceptarKeyPressed(evt);
            }
        });

        jbCancelar.setFont(resourceMap.getFont("jbCancelar.font")); // NOI18N
        jbCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/button_cancel.png"))); // NOI18N
        jbCancelar.setMnemonic('C');
        jbCancelar.setText(resourceMap.getString("jbCancelar.text")); // NOI18N
        jbCancelar.setName("jbCancelar"); // NOI18N
        jbCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jcbRoles, javax.swing.GroupLayout.PREFERRED_SIZE, 320, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(255, Short.MAX_VALUE)
                .addComponent(jbAceptar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbCancelar)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 20, Short.MAX_VALUE)
                    .addComponent(jcbRoles, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 330, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbAceptar)
                    .addComponent(jbCancelar))
                .addContainerGap())
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbAceptarActionPerformed
           if(this.jcbRoles.getSelectedIndex()>=0){
               RolTO rol=this.obtenerRol(this.jcbRoles.getSelectedItem().toString());
               try {
                DetalleUsuarioRolTO ne= new DetalleUsuarioRolTO();
                ne.setId_rol(rol.getCodigo());
                ne.setDescripcion(rol.getDescripcion());
                ne.setFecha(new Date());
                this.padreasignar.agregaDetalle(ne);
                padreasignar.actualizaTablaDetalle();
                this.jcbRoles.requestFocus();             
                } catch (Exception ex) {
                Logger.getLogger(jdRegistrarFuncion.class.getName()).log(Level.SEVERE, null, ex);
                }
           }
      
}//GEN-LAST:event_jbAceptarActionPerformed

    private void jcbRolesKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jcbRolesKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER ){  
            this.jbAceptar.requestFocus();
        }
    }//GEN-LAST:event_jcbRolesKeyPressed

    private void jbCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCancelarActionPerformed

        this.dispose();
}//GEN-LAST:event_jbCancelarActionPerformed

    private void jcbRolesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbRolesActionPerformed
       if(this.jcbRoles.getSelectedIndex()>=0){
             this.cargarDetalles();
       }
    }//GEN-LAST:event_jcbRolesActionPerformed

    private void jbAceptarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jbAceptarKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER ){
            this.jbAceptar.doClick();
        }
    }//GEN-LAST:event_jbAceptarKeyPressed

    /**
    * @param args the command line arguments
    */
   

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private administracion.BeanBinding.cboBeanBindingRoles cboBeanBindingRoles1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    public javax.swing.JButton jbAceptar;
    private javax.swing.JButton jbCancelar;
    private javax.swing.JComboBox jcbRoles;
    public javax.swing.JTable jtFunciones;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

}
